package com.ruifu.doc.service.impl;

import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.ruifu.common.po.DmFoldCont;
import com.ruifu.common.utils.BeanConverter;
import com.ruifu.doc.dto.foldcont.DmFoldContDeleteDTO;
import com.ruifu.doc.dto.foldcont.DmFoldContInsertDTO;
import com.ruifu.doc.mapper.DmFoldContMapper;
import com.ruifu.doc.service.DmDocVerService;
import com.ruifu.doc.service.DmFoldContService;
import com.ruifu.doc.vo.foldcont.DmFoldContVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 文件夹内容业务逻辑层
 *
 * @author nyr
 * @create 2019/11/11
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class DmFoldContServiceImpl implements DmFoldContService {
    @Autowired(required = false)
    private DmFoldContMapper dmFoldContMapper;

    @Autowired
    private DmDocVerService dmDocVerService;

    /**
     * 批量添加文档到文件夹
     *
     * @param dmFoldContInsertDTO
     * @param cloudUser
     */
    @Override
    public void insertPL(DmFoldContInsertDTO dmFoldContInsertDTO, CloudUser cloudUser) {
        /*获取对象id集合*/
        List<Integer> fcObjIdList = dmFoldContInsertDTO.getFcObjIdList();
        fcObjIdList.forEach(fcObjId -> {
            //将存储文件夹信息放置到内容对象中
            DmFoldCont dmFoldCont = BeanConverter.copyProperties(dmFoldContInsertDTO, DmFoldCont.class);
            //设置对象id
            dmFoldCont.setFcObjId(fcObjId);
            //设置不隐藏
            dmFoldCont.setFcHide(false);
            //存储到数据库
            dmFoldContMapper.insert(dmFoldCont, cloudUser);
        });
    }

    /**
     * 批量删除文件夹内容
     *
     * @param dmFoldContDeleteDTO 文件夹删除内容DTO
     * @param cloudUser
     */
    @Override
    public void batchDelete(DmFoldContDeleteDTO dmFoldContDeleteDTO, CloudUser cloudUser) throws Exception {
        //判断是否删除子对象
        if (dmFoldContDeleteDTO.getIsDeleteObj()) {

            //根据内容id查询对象id
            List<Long> fcObjIdList = dmFoldContMapper.findObjIdsByFoldIds(dmFoldContDeleteDTO.getFcIdList(), cloudUser);

            //判断子对象类型
            switch (dmFoldContDeleteDTO.getFcObjTypeId()) {
                case 1:
                    //删除文档对象
                    dmDocVerService.delete(fcObjIdList, cloudUser);
                    break;
                case 2:
                    log.info("删除零部件对象");
                    break;
                default:
                    log.error("异常的子对象类型,请联系系统管理员进行修复");
                    throw new RuntimeException("异常的子对象类型,请联系系统管理员进行修复");
            }
        }
        //删除文件夹内容
        dmFoldContMapper.batchDelete(dmFoldContDeleteDTO.getFcIdList(), cloudUser);
    }

    /**
     * 根据文件夹id查询文件夹内容
     *
     * @param fcFoldId    文件夹id
     * @param fcObjTypeId 文件夹内容类型id
     * @param cloudUser
     * @return
     */
    @Override
    public List<DmFoldContVO> findListByFcFoldId(Long fcFoldId, Long fcObjTypeId, CloudUser cloudUser) {
        List<DmFoldCont> dmFoldContList = dmFoldContMapper.findListByFcFoldId(fcFoldId, fcObjTypeId, cloudUser);
        return BeanConverter.copyList(dmFoldContList, DmFoldContVO.class);
    }
}
